# Load packages
# install.packages("readstata13")
library(readstata13)
library(ggplot2)
library(ggthemes)
master <- read.dta13("./MASTER.dta")

pdf("./distributions.pdf", width = 8, height = 6)

# Set up a 2x3 layout
par(mfrow = c(2, 3))

# Create the plots

## Gender: First plot of Figure B1. Distribution of Data on Respondents' 
## Demographics, Income, and Partisanship

# Calculate proportions
counts <- table(master$female)
proportions <- counts / sum(counts)

# Set up barplot and capture the midpoints
midpoints <- barplot(height = proportions * 100, 
                     names.arg = c("0 (Male)", "1 (Female)"), 
                     ylim = c(0, 60),
                     main = "Gender",
                     ylab = "Percent",
                     cex.names = 0.9)

# Add percentages on top of bars using the midpoints
text(midpoints, proportions * 100 + 2, 
     labels = sprintf("%.2f%%", proportions * 100), 
     cex = 0.8, pos = 3)


## Age: Second plot of Figure B1. Distribution of Data on Respondents' 
## Demographics, Income, and Partisanship

# Calculate proportions
counts <- table(master$age)
proportions <- counts / sum(counts)

# Set up barplot without x-axis (xaxt = "n")
midpoints <- barplot(height = proportions * 100, 
                     ylim = c(0, 2.1),
                     main = "Age (in Years)",
                     ylab = "Percent",
                     xaxt = "n",
                     cex.names = 0.8) 

# Assuming age ranges from 16 to some upper limit (e.g., 116). Adjust as per your data.
age_breaks <- seq(16, 110, by = 8)

# Draw custom x-axis
axis(1, at = seq(min(midpoints), max(midpoints), length.out = length(age_breaks)), 
     labels = age_breaks)


## Income: Third plot of Figure B1. Distribution of Data on Respondents' 
## Demographics, Income, and Partisanship

# Calculate proportions
counts <- table(master$income)
proportions <- counts / sum(counts)

# Set up barplot without x-axis (xaxt = "n")
midpoints <- barplot(height = proportions * 100, 
                     main = "Income",
                     names.arg = c("1 (Low)", "2 (Middle)", "3 (High)"), 
                     ylim = c(0, 60),
                     ylab = "Percent",
                     cex.names = 0.8)  # Suppress default x-axis

# Add percentages on top of bars using the midpoints
text(midpoints, proportions * 100 + 2, 
     labels = sprintf("%.2f%%", proportions * 100), 
     cex = 0.8, pos = 3)


## Education: Fourth plot of Figure B1. Distribution of Data on Respondents' 
## Demographics, Income, and Partisanship

# Calculate proportions
counts <- table(master$education)
proportions <- counts / sum(counts)

# Set up barplot without x-axis (xaxt = "n")
midpoints <- barplot(height = proportions * 100, 
                     main = "Education",
                     names.arg = c("1 (Primary)", "2 (Secondary)", "3 (Post)"), 
                     ylim = c(0, 50),
                     ylab = "Percent",
                     cex.names = 0.7) 

# Add percentages on top of bars using the midpoints
text(midpoints, proportions * 100 + 2, 
     labels = sprintf("%.2f%%", proportions * 100), 
     cex = 0.8, pos = 3)


## Partisan: Fifth plot of Figure B1. Distribution of Data on Respondents' 
## Demographics, Income, and Partisanship

# Calculate proportions
counts <- table(master$partisan)
proportions <- counts / sum(counts)

# Set up barplot without x-axis (xaxt = "n")
midpoints <- barplot(height = proportions * 100, 
                     main = "Partisanship",
                     names.arg = c("1 (Do Not Identify)", "2 (Identify with a Party)"), 
                     ylim = c(0, 60),
                     ylab = "Percent",
                     cex.names = 0.8) 

# Add percentages on top of bars using the midpoints
text(midpoints, proportions * 100 + 2, 
     labels = sprintf("%.2f%%", proportions * 100), 
     cex = 0.8)

dev.off()

# Reset graphical parameters
par(mfrow=c(1, 1))



## Figure B2: Respondent's Self-Placement on Left-Right Ideology Scale 
## From 0 (Left) to 10 (Right)

## Keeping only one observation for each respondent
master_unique_id <- master[!duplicated(master$id), ]
pdf("./lrs_distribution.pdf", width = 8, height = 6)

# Calculate proportions
counts <- table(master_unique_id$lrs_normal)
proportions <- counts / sum(counts)

# Set up barplot without x-axis (xaxt = "n")
midpoints <- barplot(height = proportions * 100, 
                     ylim = c(0, 20),
                     ylab = "Percent")  

dev.off()

## Left-Right Party Position

pdf("./lrparty_distribution.pdf", width=8, height=6)

# Calculate proportions
counts <- table(master$lrparty_normal)
proportions <- counts / sum(counts)

# Set up barplot without x-axis (xaxt = "n")
midpoints <- barplot(height = proportions * 100, 
                     ylim = c(0, 12),
                     ylab = "Percent")  

dev.off()
